package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modal.ProductModal;
import modal.UserModal;
import dao.BidsDAO;
import dao.ProductsDAO;
@WebServlet("/CreateProductController")

public class CreateProductController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor. 
	 */
	public CreateProductController() {

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession();
			UserModal user;
			if (session.getAttribute("user")!=null){
				ProductsDAO pdao= new ProductsDAO();
				
				user=(UserModal) session.getAttribute("user");
				pdao.addProduct(request.getParameter("name"),request.getParameter("price")+" "+request.getParameter("currency"),user.getUserId(),request.getParameter("description"), request.getParameter("photoUrl"));    
				
				request.getRequestDispatcher("/HomeController").forward(request, response);
			}else{
				request.getRequestDispatcher("/login.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



